
const WebSocketServer = require('ws').Server;
const wss = new WebSocketServer({port:1313});
let count = 0;

let locationMess = [];
wss.on('connection',function(ws){
	count++
	ws.send(JSON.stringify({		
		type:'assign',
		userId:count
	}))
	
	ws.on('close',function(){
		count--
	})
	
	ws.on('message',function(data){		
		let message = JSON.parse(data);
		locationMess.push(message)
		wss.broadcast(locationMess)
	})
})
wss.broadcast = function(data){
	wss.clients.forEach(function each(client){
		client.send(JSON.stringify(data))
	})
}

module.exports = wss;